package com.jcl.elasticsearch.resume.logic;

import com.jcl.core.utils.BaseUtils;
import com.jcl.elasticsearch.resume.model.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ResumeBaseService
 * @Description TODO
 * @Author yyliu
 * @Date 2018/11/13 10:20
 * @Version 1.0
 **/
@Component
public class ResumeExportLogic {

    @Autowired
    private SearchResumeLogic                 searchResumeLogic;

    @Autowired
    private SearchResumeWorkLogic             searchResumeWorkLogic;

    @Autowired
    private SearchResumeEduLogic              searchResumeEduLogic;

    @Autowired
    private SearchResumeProjectLogic          searchResumeProjectLogic;

    @Autowired
    private ResumeExternalMsgLogic            resumeExternalMsgLogic;

    private static SimpleDateFormat           sdf_ymd = new SimpleDateFormat("yyyy-MM-dd");

    /* *
     * @Author yyliu
     * @Description //TODO
     * @Date 9:16 2018/10/26
     * @Param [entity]
     * @return java.lang.Integer
     **/
    public SearchResume exportSearchResume(Map<String,String> entity){

        SearchResume searchResume = new SearchResume();
        searchResume.setRealname(entity.get("realname"));
        searchResume.setSex(entity.get("sex"));
        searchResume.setMobile(entity.get("mobile"));
        searchResume.setEmail(entity.get("email"));
        searchResume.setBirthday(StringUtils.isNotBlank(entity.get("birthday"))?sdf_ymd.parse(entity.get("birthday"),new ParsePosition(0)):null);
        searchResume.setUpdateDate(StringUtils.isNotBlank(entity.get("update_date"))?sdf_ymd.parse(entity.get("update_date"),new ParsePosition(0)):null);
        searchResume.setCurrentSalary(entity.get("current_salary"));
        searchResume.setDesireSalaryStart(StringUtils.isNotBlank(entity.get("desire_salary_start"))?Integer.valueOf(entity.get("desire_salary_start")):null);
        searchResume.setDesireSalaryEnd(StringUtils.isNotBlank(entity.get("desire_salary_end"))?Integer.valueOf(entity.get("desire_salary_end")):null);
        searchResume.setEvaluate(entity.get("evaluate"));
        searchResume.setArrivalInfo(entity.get("arrival_info"));
        searchResume.setNativePlace(entity.get("native_place"));
        searchResume.setWorkyear(StringUtils.isNotBlank(entity.get("workyear"))?Integer.valueOf(entity.get("workyear")):null);
        searchResume.setWorkLastCompany(entity.get("work_last_company"));
        searchResume.setWorkLastPosition(entity.get("work_last_position"));
        searchResume.setEduLastEducation(entity.get("edu_last_education"));
        searchResume.setEduLastSchool(entity.get("edu_last_school"));
        searchResume.setEduLastMajor(entity.get("edu_last_major"));
        searchResume.setSourceSub(entity.get("source_sub"));
        searchResume.setCurrentCity(entity.get("current_city"));
        searchResume.setCurrentAddress(entity.get("current_address"));
        searchResume.setApplyOtherJid(entity.get("apply_other_jid"));
        searchResume.setCreatetime(new Date());
        searchResumeLogic.add(searchResume);
        Integer id = searchResume.getId();
        String externalMsg = entity.get("external_msg");
        String source = entity.get("source_sub");
        if(StringUtils.isNotBlank(externalMsg)){
            resumeExternalMsgLogic.changeExternalSearchResumeMsg(id, source, externalMsg , null);
        }
        Map<String, Object> beanMap = BaseUtils.convertToMap(searchResume);
        //elasticProxy.addSearchResume(beanMap);
        return searchResume;
    }

    public void exportSearchResumeProject(Map<String,String> entity, Integer resumeId){

        SearchResumeProject searchResumeProject = new SearchResumeProject();
        searchResumeProject.setResumeId(resumeId);
        searchResumeProject.setProjectName(entity.get("projectname"));
        searchResumeProject.setDescription(entity.get("description"));
        searchResumeProject.setStartDate(entity.get("start_date"));
        searchResumeProject.setEndDate(entity.get("end_date"));
        searchResumeProject.setDuty(entity.get("duty"));
        List<SearchResumeProject> projects = new ArrayList<>();
        projects.add(searchResumeProject);
        searchResumeProjectLogic.batchInsert(projects);
    }


    public void exportSearchresumeEdu(Map<String,String> entity, Integer resumeId){
        SearchResumeEdu searchResumeEdu = new SearchResumeEdu();
        searchResumeEdu.setResumeId(resumeId);
        searchResumeEdu.setSchool(entity.get("school"));
        searchResumeEdu.setDegree(entity.get("degree"));
        searchResumeEdu.setMajor(entity.get("major"));
        searchResumeEdu.setStartDate(entity.get("start_date"));
        searchResumeEdu.setEndDate(entity.get("end_date"));
        List<SearchResumeEdu> edus = new ArrayList<>();
        edus.add(searchResumeEdu);
        searchResumeEduLogic.batchInsert(edus);
    }

    /* *
     * @Author yyliu
     * @Description //TODO
     * @Date 9:20 2018/10/26
     * @Param [entity]
     * @return void
     **/
    public void exportSearchResumeWork(Map<String,String> entity, Integer resumeId){

        SearchResumeWork searchResumeWork = new SearchResumeWork();
        searchResumeWork.setResumeId(resumeId);
        searchResumeWork.setCompany(entity.get("company"));
        searchResumeWork.setPosition(entity.get("position"));
        searchResumeWork.setDuty(entity.get("duty"));
        searchResumeWork.setStartDate(entity.get("start_date"));
        searchResumeWork.setEndDate(entity.get("end_date"));
        List<SearchResumeWork> searchResumeWorks = new ArrayList<>();
        searchResumeWorks.add(searchResumeWork);
        searchResumeWorkLogic.batchInsert(searchResumeWorks);
    }


}
